package com.chenglu.android.mine.wallet.mvp.presenter;





import android.accounts.NetworkErrorException;

import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.base.BaseView;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.AmountTypeBean;
import com.chenglu.android.mine.bean.BeansDetailBean;
import com.chenglu.android.mine.bean.MoneyBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.wallet.mvp.contract.WalletContract;
import com.chenglu.android.mine.wallet.mvp.model.WalletModel;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class WalletPresenter extends BasePresenter<WalletContract.View> implements WalletContract.WalletPresenter {

     private  WalletContract.WalletModel model;

    public WalletPresenter(){
        model = new WalletModel();
    }



    @Override
    public void getMoneyDetails(String token) {
        if(!isViewAttached()){
            return;
        }

        model.getMoneyDetails(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<MoneyBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull MoneyBean bean) {
                        //每次进来都会刷新token
                        if(bean.getCode().equals(Constant.HTTPCODE200)){
                            mView.getMoneyDetails(bean);
                        }else {
                            mView.getMoneyDetails(bean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
//                        apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }





    @Override
    public void getAmountTypeList(String token, Map<String,String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getAmountTypeList(token, map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AmountTypeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull AmountTypeBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getAmountTypeList(bean);
                        }else {
                            mView.getAmountTypeList(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


}
